package com.example.qingyiapp.service.presenter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;


import com.example.qingyiapp.service.entify.IntrodutioBean;
import com.example.qingyiapp.service.entify.Databean;
import com.example.qingyiapp.service.entify.FindChoicenessOneBean;
import com.example.qingyiapp.service.entify.FindChoicenessTwoBean;
import com.example.qingyiapp.service.entify.FindFriend;
import com.example.qingyiapp.service.entify.FindSynamicBean;
import com.example.qingyiapp.service.entify.FindTJBean;
import com.example.qingyiapp.service.entify.FindWaitYouBean;
import com.example.qingyiapp.service.entify.FriendSquareBean;
import com.example.qingyiapp.service.entify.FriendXxBean;
import com.example.qingyiapp.service.manager.DataManager;
import com.example.qingyiapp.service.view.IChildView;
import com.example.qingyiapp.service.view.IFindView;
import com.example.qingyiapp.service.view.IView;
import com.example.qingyiapp.ui.activity.fragment.fragment_AddressUI.address_bean.JavaBean_lv;
import com.example.qingyiapp.ui.activity.fragment.fragment_AddressUI.address_bean.JavaBean_lv2;
import com.example.qingyiapp.ui.activity.fragment.fragment_AddressUI.address_bean.JavaBean_lv3;
import com.example.qingyiapp.ui.activity.fragment.fragment_AddressUI.address_bean.JavaBean_msg;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * @author ：created by 申德丰
 * Create Date ：2019/9/15 10:35
 * Package_Name : QingYiApp
 */
public class PersenterMannaer  implements IRequestPersenter {

    private Databean databean;
   private DataManager dataManager;
    private Context context;
    private String url;
    private String request;
    private IChildView iChildView;

    private IFindView iFindView;
    private CompositeSubscription compositeSubscription;

    JavaBean_lv javaBean_lv;
    JavaBean_lv2 javaBean_lv2;
    JavaBean_lv3 javaBean_lv3;
    JavaBean_msg javaBean_lv4;
    FriendSquareBean friendSquareBean;
    FindFriend findFriend;
    IntrodutioBean bintrodutioBean;

    public PersenterMannaer(Context context, String url) {
        this.context = context;
        this.url = url;

    }

    @Override
    public void getMessage() {
        compositeSubscription.add(dataManager.getdatamanager()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<Databean>() {
            @Override
                public void onCompleted() {

                iChildView.onSuccess(databean);
            }

            @Override
            public void onError(Throwable e) {
                iChildView.onError(e.getMessage());
            }

            @Override
            public void onNext(Databean s) {
                databean=s;
            }
        }));
    }

    @Override
    public void getAddress_lv() {
        compositeSubscription.add(dataManager.getAddress_lv()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<JavaBean_lv>() {
                    @Override
                    public void onCompleted() {

                        iChildView.ongetAddress_lvData(javaBean_lv);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("TagA",e.getMessage());
                        iChildView.onError(e.getMessage());
                    }

                    @Override
                    public void onNext(JavaBean_lv s) {
                        javaBean_lv=s;
                        Log.i("TagA","onNext:"+s.getMsg());

                    }
                }));
    }

    @Override
    public void getfriendsquare(int page,int num) {
        compositeSubscription.add(dataManager.getfriendsquare(page,num)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FriendSquareBean>() {
                    @Override
                    public void onCompleted() {

                        iChildView.ongetfriendSquareBean(friendSquareBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        iChildView.onError(e.getMessage());
                    }

                    @Override
                    public void onNext(FriendSquareBean s) {
                        friendSquareBean=s;
                    }
                }));
    }

    @Override
    public void getfindfriend(int startidx, int offset) {
        compositeSubscription.add(dataManager.getfindfriend(startidx,offset)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FindFriend>() {
                    @Override
                    public void onCompleted() {

                        iChildView.ongetfindfriend(findFriend);
                    }

                    @Override
                    public void onError(Throwable e) {
                        iChildView.onError(e.getMessage());
                    }

                    @Override
                    public void onNext(FindFriend s) {
                        findFriend=s;
                    }
                }));
    }


    @Override
    public void getFindRecommendJson() {
        compositeSubscription.add(dataManager.getFindRecommendJson()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FindTJBean>() {
                    @Override
                    public void onCompleted() {
                        Log.d("###", "onCompleted: ok");

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d("###", "onError: "+e.getMessage());
                    }

                    @Override
                    public void onNext(FindTJBean findTJBean) {
                        Log.d("###", "onNext: ");

                        iFindView.getjsonstring(findTJBean);
                    }
                }));


    }

    @Override
    public void getFindChoicenessBean() {
        compositeSubscription.add(dataManager.getgetFindChoicenessBean()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FindChoicenessOneBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(FindChoicenessOneBean findChoicenessOneBean) {
                        iFindView.getChoicenessOneBean(findChoicenessOneBean);
                    }
                }));
    }

    @Override
    public void getFindChoicenessTwoBean() {
        compositeSubscription.add(dataManager.getFindChoicenessTwoBean()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FindChoicenessTwoBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(FindChoicenessTwoBean findChoicenessTwoBean) {
                        iFindView.getChoicenessTwoBean(findChoicenessTwoBean);

                    }
                }));
    }

    @Override
    public void getFindWaitYouBean() {
        compositeSubscription.add(dataManager.getWaitYouBean()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FindWaitYouBean>() {

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(FindWaitYouBean findWaitYouBean) {
                        iFindView.getFindWaitYou(findWaitYouBean);
                    }
                }));
    }

    @Override
    public void getFindSynamicBean() {
        compositeSubscription.add(dataManager.getFindSynamicBean()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FindSynamicBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(FindSynamicBean findSynamicBean) {
                        iFindView.getFindSynamic(findSynamicBean);
                    }
                }));
    }


    @Override
    public void getFriendxxBean(String uid) {
        compositeSubscription.add(dataManager.getfriendxxbean(uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FriendXxBean>() {
                    @Override
                    public void onCompleted() {

                    }


                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(FriendXxBean findSynamicBean) {
                        iChildView.getfriendxxbean(findSynamicBean);
                    }
                }));
    }

    @Override
    public void getAddress_lv2() {

        compositeSubscription.add(dataManager.getAddress_lv2()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<JavaBean_lv2>() {
                    @Override
                    public void onCompleted() {

                        iChildView.ongetAddress_lvData2(javaBean_lv2);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("TagA",e.getMessage());
                        iChildView.onError(e.getMessage());
                    }

                    @Override
                    public void onNext(JavaBean_lv2 s) {
                        javaBean_lv2=s;
                        Log.i("TagA","onNext:"+s.getMsg());

                    }
                }));

    }

    @Override
    public void getAddress_lv3() {
        compositeSubscription.add(dataManager.getAddress_lv3()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<JavaBean_lv3>() {
                    @Override
                    public void onCompleted() {

                        iChildView.ongetAddress_lvData3(javaBean_lv3);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("TagA",e.getMessage());
                        iChildView.onError(e.getMessage());
                    }

                    @Override
                    public void onNext(JavaBean_lv3 s) {
                        javaBean_lv3=s;
                        Log.i("TagA","onNext:"+s.getMsg());

                    }
                }));
    }

    @Override
    public void getAddress_lv4(String pid) {
        compositeSubscription.add(dataManager.getAddress_lv4(pid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<JavaBean_msg>() {
                    @Override
                    public void onCompleted() {

                        iChildView.ongetAddress_lvData4(javaBean_lv4);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("TagA",e.getMessage());
                        iChildView.onError(e.getMessage());
                    }

                    @Override
                    public void onNext(JavaBean_msg s) {
                        javaBean_lv4=s;
                        Log.i("TagA","onNext:"+s.getMsg());

                    }
                }));
    }

    @Override
    public void getIntroduction() {
        compositeSubscription.add(dataManager.getIntroductionBean()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<IntrodutioBean>() {
                    @Override
                    public void onCompleted() {

                        iChildView.getIntrodution1(bintrodutioBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        iChildView.onError(e.getMessage());
                        Log.d("TagZ",e.getMessage());
                    }

                    @Override
                    public void onNext(IntrodutioBean introdutioBean) {
                        bintrodutioBean=introdutioBean;
                    }

                }));
    }

    @Override
    public void onCreate() {


        compositeSubscription=new CompositeSubscription();
        dataManager=new DataManager(context,url);
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onStop() {

        compositeSubscription.unsubscribe();//取消注册
    }

    @Override
    public void pause() {

    }

    @Override
    public void attchview(IView view) {
        iChildView= (IChildView) view;
    }

    @Override
    public void attchview2(IView view) {
        iFindView= (IFindView) view;
    }


    @Override
    public void attchincommingIntent(Intent intent) {

    }

    @Override
    public void attchviewa(IView view) {
        iChildView= (IChildView) view;
    }

    @Override
    public void getlv4(IView view) {
        iChildView= (IChildView) view;
    }
}
